package piechowicz.szymon.fft;

public class Complex
{
    
    public double re;
    public double im;
    
    public Complex(final double re, final double im)
    {
        this.re = re;
        this.im = im;
    }
    
    public Complex(final double re)
    {
        this.re = re;
        this.im = 0;
    }
    
    public Complex()
    {
        this.re = 0;
        this.im = 0;
    }
    
    public void set(final Complex that)
    {
        re = that.re;
        im = that.im;
    }
    
    public void set(final double re, final double im)
    {
        this.re = re;
        this.im = im;
    }
    
    public double abs()
    {
        return Math.hypot(re, im);
    }
    
    public double arg()
    {
        return Math.atan2(im, re);
    }
    
    public Complex plus(final Complex that)
    {
        re += that.re;
        im += that.im;
        return this;
    }

    public Complex plus(final double that_re)
    {
        re += that_re;
        return this;
    }
    
    public Complex minus(final Complex that)
    {
        re -= that.re;
        im -= that.im;
        return this;
    }
    
    public Complex minus(final double that_re)
    {
        re -= that_re;
        return this;
    }
    
    public Complex multiply(final Complex that)
    {
        final double new_re = re * that.re - im * that.im;
        im = im * that.re + re * that.im;
        re = new_re;
        return this;
    }
    
    public Complex multiply(final double that_re)
    {
        re *= that_re;
        im *= that_re;
        return this;
    }
    
    public Complex divide(final Complex that)
    {
        final double new_re = (re * that.re + im * that.im)
                / (that.re * that.re + that.im * that.im);
        im = (im * that.re - re * that.im)
                / (that.re * that.re + that.im * that.im);
        re = new_re;
        return this;
    }

    public Complex divide(final double that_re)
    {
        re /= that_re;
        im /= that_re;
        return this;
    }
    
    public static Complex exp(final Complex that)
    {
        return new Complex(Math.exp(that.re) * Math.cos(that.im),
                Math.exp(that.re) * Math.sin(that.im));
    }
    
}
